package com.tydic.serviceprovider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Description: consul服务注册
 * @Author: xuwenguang
 * @CreateDate: 2019/3/6 9:33
 * @UpdateUser: xuwenguang
 * @UpdateDate: 2019/3/6 9:33
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@RefreshScope//不加此注解需要重启服务配置文件才会更新
@RestController
public class HelloController {
    @Value("${test.name}")
    private String testName;
    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping(value="/hello",method = {RequestMethod.GET})
    public String hello(){
        List<String> services=discoveryClient.getServices();
        for (String service : services) {
            System.out.println("service name:"+service);
        }
        int time=new Random().nextInt(3000);
        try {
            Thread.sleep(Long.parseLong(String.valueOf(time)));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello consul";

    }

    @RequestMapping(value="/",method = {RequestMethod.GET})
    public String healthCheck(){
        return "hello consul";
    }
    @RequestMapping(value="/config",method = {RequestMethod.GET})
    public String getConfig(){
        return this.testName;
    }
}
